import { create } from 'zustand'

import { User } from '@/service/models/UserModel'

const UUID_KEY = 'uuid'

interface AuthState {
  uuid: string | null
  user: User
}

interface AuthAction {
  setUser: (user: User) => void
  login: (uuid: string) => void
  logout: () => void
}

export const useAuthStore = create<AuthState & AuthAction>()(set => ({
  uuid: localStorage.getItem(UUID_KEY),
  user: {} as User,

  setUser: (user: User) => set(() => ({ user })),
  login: (uuid: string) => {
    localStorage.setItem(UUID_KEY, uuid)

    return set(() => ({ uuid }))
  },
  logout: () => {
    localStorage.removeItem(UUID_KEY)
    return set(() => ({ uuid: null }))
  },
}))
